/**
 * 简历窄列表
 */
import React from 'react';
import { Table } from 'antd';
import { TableFilter } from '@/components';
import { getDisplayText, getDict } from '@/utils/dataDictUtil';
import { eventTrack } from '@/utils/track';

import { Operates } from '../components';
import { RecommendOperate, TransferOperate, ThumbCommentGroup, TalentsBlackListOperate } from './index';
import {
    renderShortDate,
    renderResumeStatus,
    renderNameWithReadFlagTitle,
    renderNameWithReadFlag,
    renderTalentDegree,
    renderLastPost
} from '../renderUtil';
import { RESUME_STATUS, SEX, LAST_POST_STATUS } from '../dictionary';

const ResumeCommonTable = ({
    loading,
    dataSource,
    condition,
    onJumpDetail,
    onRowChange,
    onRefresh,
    isPrincipal,
    isLauncher,
    channelDict,
    recruitDetail,
    ...rest
}) => {
    const [agreeTypeList, setAgreeTypeList] = React.useState([]); // 学历
    // 字典获取
    React.useEffect(() => {
        getDict('agreeType').then(ret => setAgreeTypeList(ret));
    }, []);
    const handleChange = (id, res) => {
        if (onRowChange) {
            onRowChange(id, res);
        }
    };
    const columns = [
        {
            title: renderNameWithReadFlagTitle('姓名'),
            dataIndex: 'readFlag',
            render: (value, record) => {
                const { talentName = '--', showReadFlag, hasBlackList } = record;
                return renderNameWithReadFlag(talentName, value, showReadFlag, hasBlackList);
            }
        },
        {
            title: '性别',
            dataIndex: 'talentSex',
            width: 80,
            filteredValue: condition.talentSex ? [condition.talentSex] : [],
            filters: SEX,
            filterMultiple: false,
            filterDropdown: filterProps => <TableFilter {...filterProps} />,
            render: sex => getDisplayText(SEX, sex)
        },
        {
            title: '学历',
            dataIndex: 'talentDegree',
            width: 100,
            filteredValue: condition.talentDegree ? [condition.talentDegree] : [],
            filters: agreeTypeList,
            filterMultiple: false,
            filterDropdown: filterProps => <TableFilter {...filterProps} />,
            render: (val, record) =>
                renderTalentDegree(getDisplayText(agreeTypeList, val), record.talentEduDetailVOList)
        },
        {
            title: '最近工作',
            dataIndex: 'lastPost',
            render: (val, record) => renderLastPost(val, record.talentWorkDetailVOList)
        },
        {
            title: '在职情况',
            dataIndex: 'lastPostStatus',
            filteredValue: condition.lastPostStatus ? [condition.lastPostStatus] : [],
            filters: LAST_POST_STATUS,
            filterMultiple: false,
            filterDropdown: filterProps => <TableFilter {...filterProps} />,
            render: val => getDisplayText(LAST_POST_STATUS, val)
        },
        {
            title: '渠道',
            dataIndex: 'notesSourceList',
            filteredValue: condition.notesSourceList || [],
            filters: channelDict,
            filterMultiple: true,
            render: (val, record) => getDisplayText(channelDict, record.notesSource)
        },
        {
            title: '申请时间',
            dataIndex: 'applyTime',
            width: 90,
            render: val => renderShortDate(val)
        },
        {
            title: '状态',
            dataIndex: 'status',
            width: 80,
            filteredValue: condition.status ? [condition.status] : [],
            filters: RESUME_STATUS,
            filterMultiple: false,
            filterDropdown: filterProps => <TableFilter {...filterProps} />,
            render: renderResumeStatus
        },
        {
            title: '操作',
            dataIndex: 'action',
            width: 230,
            onCell: () => ({
                onClick: e => {
                    e.stopPropagation();
                }
            }),
            render: (_, record) => {
                const {
                    praiseEmployeeNameList = [],
                    trampleEmployeeNameList = [],
                    evaluateType,
                    id,
                    talentCode,
                    hasBlackList,
                    resumeStep,
                    status,
                    recruitList = []
                } = record;
                const defaultNoticeEmployee = new Set();
                recruitList.forEach(item => {
                    defaultNoticeEmployee.add(item.launchEmployeeCode);
                    defaultNoticeEmployee.add(item.principalEmployeeCode);
                });
                const operates = !hasBlackList ? [
                    <ThumbCommentGroup
                        onChange={handleChange}
                        initState={{ praiseEmployeeNameList, trampleEmployeeNameList, evaluateType }}
                        resumeId={id}
                        record={record}
                        onRefresh={onRefresh}
                    ></ThumbCommentGroup>,
                    <RecommendOperate
                        resumeId={id}
                        onConfirm={onRefresh}
                        weedOutDisabled={!(isPrincipal || isLauncher)}
                    />,
                    <TransferOperate resumeId={id} />,
                    <TalentsBlackListOperate
                        operateType="add"
                        talentCode={talentCode}
                        onRefresh={onRefresh}
                        defaultNoticeEmployee={Array.from(defaultNoticeEmployee)}
                        resumeStep={resumeStep}
                        messageType={[3].includes(status) ? 0 : 2}
                    />
                ] : [
                    <TalentsBlackListOperate
                        operateType="remove"
                        talentCode={talentCode}
                        onRefresh={onRefresh}
                        defaultNoticeEmployee={Array.from(defaultNoticeEmployee)}
                        messageType={[3].includes(status) ? 0 : 2} // 同人才库加入黑名单判断条件，简历阶段非淘汰type为2
                        resumeStep={resumeStep}
                    />
                ];
                return (
                    // onChange 返回 新的{ praiseEmployeeNameList , trampleEmployeeNameList, evaluateType, status }
                    <Operates operates={operates} />
                );
            }
        }
    ];

    React.useEffect(() => {
        const sourceListText = condition?.notesSourceList.map(item => getDisplayText(channelDict, item)).join(',');
        eventTrack({
            eventName: 'personnel_management_recruitment_demand_list_detail_resume_column_screen_click',
            pageName: 'personnel_management_recruitment',
            eventProps: {
                click_context: `${recruitDetail.dutyName}-渠道筛选-${sourceListText}`
            }
        });
    }, [JSON.stringify(condition.notesSourceList)]);

    return (
        <Table
            {...rest}
            rowKey="id"
            bordered={false}
            columns={columns}
            loading={loading}
            dataSource={dataSource}
            pagination={false}
            onRow={record => ({
                onClick: () => {
                    const { id } = record;
                    if (onJumpDetail) onJumpDetail(id, record);
                }
            })}
        />
    );
};
export default ResumeCommonTable;
